<?php
class shop_applyControl extends skymvc{
	
	
	public function __construct(){
		parent::__construct();	
	}
	
	public function onInit(){
		M("login")->checkLogin();
	}
	
	public function onDefault(){
		$data=M("shop")->selectRow(array("where"=>"userid=".M("login")->userid)); 	
		$cat_list=M("category")->children(0,MODEL_SHOP_ID);
		$this->smarty->assign(array(
			"data"=>$data,
			 
			 
			"cat_list"=>$cat_list
			
		));
			$this->smarty->display("shop_apply/index.html");
		}
		
	public function onSave(){
			
			$data=M("shop")->postData();
			$data["userid"]=M("login")->userid;
	 		$data['siteid']=SITEID;
			if(empty($data['shopname'])) $this->goall($this->lang['shopname_is_empty'],1);
			if(M("shop")->selectRow(array("where"=>"userid=".M("login")->userid))){
					$this->goall('你已经提交申请，请等待审核',1,0,"/index.php");
			}
			if(empty($data['catid'])){
				$this->goALl("请选择店铺类型",1);
			}
			if(empty($data['telephone'])){
				$this->goALl("电话不能为空",1);
			}
			$data["dateline"]=time();
			
			$data["sc_id"]=array_lastnum(get_post("sc_id","i"));
			$shopid=M("shop")->insert($data);
			$sdata['shopid']=$shopid;
			$sdata['dateline']=time();
			M("shop_data")->insert($sdata);
			//绑定用户
			M("user")->update(array("shopid"=>$shopid),"userid=".M("login")->userid);
			$_SESSION['ssuser']=M("user")->selectRow("userid=".M("login")->userid);
			$this->goall("申请成功，请等待审核",0,0,"/index.php");
	}
	/*扫街*/
	public function onSJ(){
		$this->smarty->display("shop_apply/sj.html");
	}
	
	public function onSjSave(){
		$data['siteid']=SITEID;
		$data["shopname"]=post("shopname","h");
		$data["shopurl"]=post("shopurl","h");
	 	
		$data["nickname"]=post("nickname","h");
		 
		$data["logo"]=post("logo","h");
 		$data["info"]=get_post("info","h");
		$data["telephone"]=get_post("telephone","h");
		$data["sc_id"]=array_lastnum(get_post("sc_id","i"));
		$data['catid']=get_post('catid','i');
		$data["address"]=get_post("address","h");
	 
		$data["send_time"]=get_post("send_time","i");
		$data["send_startprice"]=round(get_post("send_startprice"),1);
		$data["send_info"]=get_post("send_info","h");
		$data["open_time"]=get_post("open_time","h");
		$data["tags"]=get_post("tags","h");
		$data['qq']=post('qq','h');
		$data['zhuying']=post('zhuying','h');
		$shop['tpl']=post('tpl','h')?post('tpl','h'):"index/";
		//gps信息
		if(post('latlng')){
			$latlng=explode(",",post('latlng'));
			$data['lat']=$latlng[0];
			$data['lng']=$latlng[1];
		}elseif(post('lat')){
			$data['lat']=post('lat');
			$data['lng']=post('lng');
		}
		if($shopid){
			
			M("shop")->update($data,"shopid='$shopid'");
			M("shop_data")->update(array("content"=>post('content','x')),"shopid=".$shopid);
			if(!M("shop_data")->selectRow("shopid=".$shopid)){
				M("shop_data")->insert(array("shopid"=>$shopid,"content"=>post('content','x'),"dateline"=>time()),"shopid=".$shopid);
			}
		}else{
			$data["dateline"]=time();
			M("shop")->insert($data);
			M("shop_data")->insert(array("shopid"=>$shopid,"content"=>post('content','x'),"dateline"=>time()),"shopid=".$shopid);
		}
		$this->goall("保存成功");
	}
	
}
?>